package cn.com.fujica.face_thymeleaf.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * @ClassName MainController
 * @Description TODO
 * @Author admin
 * @Date 2020/7/13 15:09
 * @Version 1.0
 **/

@RestController
@RequestMapping("face")
@Slf4j
public class MainController {

    /**
     * 系统首次进入页面
     *
     * @return
     */
    @GetMapping("/login")
    public ModelAndView login() {
        return new ModelAndView("login");
    }

    /**
     * 进入后台管理页面
     *
     * @param
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }

    /**
     * 进入人脸库管理
     *
     * @param
     * @return
     */
    @GetMapping("/databaseManagement")
    public ModelAndView databaseManagement() {
        ModelAndView modelAndView = new ModelAndView("databaseManagement");
        return modelAndView;
    }

    /**
     * 进入人脸添加页面
     *
     * @param
     * @return
     */
    @GetMapping("/dataAdd")
    public ModelAndView dataAdd() {
        ModelAndView modelAndView = new ModelAndView("dataAdd");
        return modelAndView;
    }

    /**
     * 进入数据管理页面
     *
     * @param
     * @return
     */
    @GetMapping("/dataManage")
    public ModelAndView dataManage() {
        ModelAndView modelAndView = new ModelAndView("dataManage");
        return modelAndView;
    }

    /**
     * 进入人员管理页面
     *
     * @param
     * @return
     */
    @GetMapping("/personManagement")
    public ModelAndView personManagement(@RequestParam(value = "setName") String setName,
                                         @RequestParam(value = "modelVersion") String modelVersion) {
        ModelAndView modelAndView = new ModelAndView("personManagement");
        if (StringUtils.isNotBlank(setName) && StringUtils.isNotBlank(modelVersion)) {
            modelAndView.addObject("message", "OK");
        } else {
            modelAndView.addObject("message", "未选择人脸库与特征版本");
        }
        modelAndView.addObject("setName", setName);
        modelAndView.addObject("modelVersion", modelVersion);
        return modelAndView;
    }

    /**
     * 进入人员添加页面
     *
     * @param
     * @return
     */
    @GetMapping("/personAdd")
    public ModelAndView personAdd(@RequestParam("setName") String setName, @RequestParam("modelVersion") String modelVersion) {

        log.info("人员添加---人脸库名称:{},特征模板:{}", setName, modelVersion);
        ModelAndView modelAndView = new ModelAndView("personAdd");
        modelAndView.addObject("setName", setName);
        modelAndView.addObject("modelVersion", modelVersion);
        return modelAndView;
    }

    /**
     * 进入feature
     *
     * @param
     * @return
     */
    @GetMapping("/feature")
    public ModelAndView feature() {
        ModelAndView modelAndView = new ModelAndView("feature");
        return modelAndView;
    }


    /**
     * 查看人脸用户的详细信息
     * @param id
     * @param setName
     * @param modelVersion
     * @return
     */
    @GetMapping("/personDetail")
    public ModelAndView personDetail(@RequestParam("id") String id,
                                     @RequestParam("setName") String setName,
                                     @RequestParam("modelVersion") String modelVersion) {
        log.info("查看人脸用户的详细信息---id: {}, setName: {}, modelVersion: {}", id, setName, modelVersion);
        ModelAndView modelAndView = new ModelAndView("personDetail");
        modelAndView.addObject("id", id);
        modelAndView.addObject("setName", setName);
        modelAndView.addObject("modelVersion", modelVersion);
        return modelAndView;
    }

    /**
     * 修改人脸用户的详细信息
     * @param id
     * @param setName
     * @param modelVersion
     * @return
     */
    @GetMapping("/personEdit")
    public ModelAndView personEdit(@RequestParam("id") String id,
                                     @RequestParam("setName") String setName,
                                     @RequestParam("modelVersion") String modelVersion) {
        log.info("查看人脸用户的详细信息---id: {}, setName: {}, modelVersion: {}", id, setName, modelVersion);
        ModelAndView modelAndView = new ModelAndView("personEdit");
        modelAndView.addObject("id", id);
        modelAndView.addObject("setName", setName);
        modelAndView.addObject("modelVersion", modelVersion);
        return modelAndView;
    }

    /**
     * 查看单个人脸库的详细信息
     *
     * @param
     * @return
     */
    @GetMapping("/dataDetail")
    public ModelAndView dataDetail(@RequestParam(value = "setName") String setName, @RequestParam(value = "modelVersion") String modelVersion) {
        log.info("setName: {}, modelVersion: {}", setName, modelVersion);
        ModelAndView modelAndView = new ModelAndView("dataDetail");
        modelAndView.addObject("setName", setName);
        modelAndView.addObject("modelVersion", modelVersion);
        return modelAndView;
    }

    @PostMapping("/uploadImage")
    public String uploadImage(@RequestParam("userImage") MultipartFile userImage) throws IOException {

        // 获取文件名
        String fileName = userImage.getOriginalFilename();

//        // 获取文件后缀
//        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 创建文件夹
        File tempFile = new File("D:\\face");

        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }

        OutputStream os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
        // 保存为临时文件 1k的数据缓存
        byte[] bs = new byte[1024];
        // 开始读取
        int len;
        InputStream inputStream = userImage.getInputStream();
        while ((len = inputStream.read(bs)) != -1) {
            os.write(bs, 0, len);
        }

        // 关闭所有连接
        os.close();
        inputStream.close();


        String url = "C://Users/FSZN/Desktop/xfr/hg.jpg";
        return url;

    }


    }
